package com.kcl.dfss.view;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.videolan.libvlc.IVideoPlayer;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcException;
import org.videolan.libvlc.util.VLCInstance;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Environment;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
	private static LibVLC vlc = null;
	private SurfaceHolder surfaceHolder = null;
	private IVideoPlayer mIVideoPlayer = null;
	public CameraView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}
	public CameraView(Context context, AttributeSet attri){
		super(context, attri);
		init(context);
	}
	public void init(Context context){
		try {
			vlc = VLCInstance.getLibVlcInstance(context);
		} catch (LibVlcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		surfaceHolder = this.getHolder();
        surfaceHolder.setFixedSize(400, 400);
        surfaceHolder.setFormat(PixelFormat.RGBA_8888);
        surfaceHolder.addCallback(this);
        setDrawingCacheEnabled(true);
        setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        
	}
	public void setVedioPlayerCallback(IVideoPlayer player){
		mIVideoPlayer = player;
	}
	
	private void attachSurfaceView(){
		vlc.attachSurface(surfaceHolder.getSurface(), mIVideoPlayer);
	}
	public void setRTSPPathAndPlay(String path){
		if(vlc.isPlaying()){
			vlc.stop();
		}
//		vlc.playMRL(path);
		vlc.playMRL(path, new String[]{":file-caching=1500",":network-caching=1500",":rtsp-caching=1000"});
//		vlc.play();
	}
	
	public void pause(){
		if(vlc.isPlaying()){
			vlc.pause();
//			vlc.stop();
		}
	}
	
	public void resume(){
		
		vlc.play();
	}
	
	//获取视频当前播放位置
	public int getPosition(){
		/*Time progress = new Time((long)vlc.getPosition());
		String currentTime = progress.toString();
		return currentTime;*/
		return (int) vlc.getTime();
	}
	
	//获取视频长度
	public long getLength(){
		/*Time allTime = new Time(vlc.getLength());
		String durationTime = allTime.toString();
		return durationTime;*/
		return vlc.getLength();
	}
	
	//快退或快进设置视频的播放位置
	public void setPosition(float pos){
		vlc.setTime((long)pos);
	}
	
	public boolean getPlayState(){
		return vlc.isPlaying();
	}
	
	public void destroy(){
		vlc.stop();
	}
	public void record(String path){
//		if(!vlc.videoIsRecordable()){
//			return;
//		}
//		if(vlc.videoIsRecording()){
//			vlc.videoRecordStop();
//		}
//		vlc.videoRecordStart(path);
	}
	
	public void setSurfaceSize(int width,int height){
		surfaceHolder.setFixedSize(width, height);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		attachSurfaceView();
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if(vlc != null){
			vlc.detachSurface();
		}
	}
	
	@SuppressLint("SimpleDateFormat")
	public String snapShot()
	{
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
			String name = df.format(new Date());
			name = Environment.getExternalStorageDirectory()+"/dfss/capture/"+name+".png";
			File file = new File(name);
			if(!file.exists())
				file.createNewFile();
//			if(vlc.takeSnapShot(name, this.getWidth(), this.getHeight())) 
//			{
//				//Toast.makeText(getApplicationContext(), "已保存", 1000).show();
//			}
//			else
//			{
//				//Toast.makeText(getApplicationContext(), "截图失败", 1000).show();
//			}
			return name;
			
		}catch (Exception e) {
			e.printStackTrace();
			
			return null;
		}
	}
	
}
